﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Core.Property;

namespace Cofe.Core.Entry
{
    /// <summary>
    /// This is used for listing volume registered CustomEntryList.
    /// </summary>
    public class EntryListDirectoryLister : IDirectoryLister
    {
         #region Constructor
        public EntryListDirectoryLister(IPropertyProvider rootPropertyProvider)
        {
            ManualEventSource = new DummyCofeSystemEventSource();
            string parseName = rootPropertyProvider.GetOneProperty<string>(CofeProperties.FullParseName);
            if (!(parseName.EndsWith("}")))
                throw new ArgumentException();
            parseName = parseName.Trim('{', '}');
            _rootPropertyProvider = rootPropertyProvider;
        }

        #endregion

        #region Methods

        public Task<IPropertyProvider> TryParsePathAsync(string path, EntryConstructionFlags flags)
        {
            string volPath = "{" + Volume.VolumeId + "}";
            if (volPath.Equals(path, StringComparison.CurrentCultureIgnoreCase))
                return Task.FromResult<IPropertyProvider>(_rootPropertyProvider);
            return Task.FromResult<IPropertyProvider>(null);
        }


        public IEnumerable<IPropertyProvider> GetRootPropertyProviders()
        {
            yield return _rootPropertyProvider;
        }

        #endregion

        #region Data

        private IVolume _volume;
        private IPropertyProvider _rootPropertyProvider;

        #endregion

        #region Public Properties

        public IManualCofeSystemEventSource ManualEventSource { get; set; }

        public string VolumePath
        {
            get { return Volume.Parameters["VolumePath"]; }
        }

        public IVolume Volume
        {
            get { return _volume; }
            set { _volume = value; }
        }

        #endregion
    }
}
